import axios from 'axios'
import { useUserStore } from '@/stores'
import { showFailToast } from 'vant'
import router from '@/router'

// axios基础配置
const baseURL = 'https://consult-api.itheima.net/'
const request = axios.create({
  baseURL,
  timeout:1000
})

// 请求拦截器，携带token
request.interceptors.request.use(
  (config) => {
    console.log(config,'confih')
    const store = useUserStore()
    if(store.user?.token){
      //登陆了
      config.headers.Authorization = `Bearer ${store.user.token}`
    }
    return config
  },
  (error) => Promise.reject(error)
)

//响应拦截器，剥离无效数据，401拦截
request.interceptors.response.use(
  (res) => {
    //后台约定，响应成功，但是code不是10000，是业务逻辑失败
    if(res.data?.code !== 10000){
      showFailToast(res.data.message)
      return Promise.reject(res.data)
    }
    //业务逻辑成功，返回响应数据，作为axios成功的结果
    return res.data?.data
  },
  (error) => {
    if(error.response.status === 401){
      //删除用户信息
      const store = useUserStore()
      store.delUser()
      //跳转登录，带上接口失效所在页面地址，登录完成后回跳使用
      router.push(`/login?returnUrl=${router.currentRoute.value.fullPath}`)
    }
    return Promise.reject(error)
  }
)

export { baseURL,request }
